import 'dart:async';

import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/enums/base_advanced_filter_type.dart';
import 'package:x3_form_core/filter_components/components/advanced_filter_date_range.dart';
import 'package:x3_form_core/filter_components/components/advanced_filter_input_text.dart';
import 'package:x3_form_core/filter_components/components/advanced_filter_input_text_range.dart';
import 'package:x3_form_core/filter_components/components/advanced_filter_multi_choice.dart';
import 'package:x3_form_core/filter_components/components/advanced_filter_single_choice.dart';
import 'package:x3_form_core/models/base_advanced_filter_model.dart';
import 'package:x3_form_core/provider/base_advanced_filter_provider.dart';

import 'components/advanced_filter_input_search_single_choice.dart';

/// author：william on 2021/4/8 10:02
/// email：362661044@qq.com
class BaseAdvancedFilter extends StatefulWidget {
  final BaseAdvancedFilterProvider baseAdvancedFilterProvider;
  final Function(Map<String, dynamic>) onComplete;

  const BaseAdvancedFilter({Key key, @required this.baseAdvancedFilterProvider, this.onComplete}) : super(key: key);

  @override
  _BaseAdvancedFilterState createState() => _BaseAdvancedFilterState();
}

class _BaseAdvancedFilterState extends State<BaseAdvancedFilter> {
  BaseAdvancedFilterProvider get provider => widget.baseAdvancedFilterProvider;
  StreamController _resetStreamController = StreamController<bool>.broadcast();

  @override
  void initState() {
    provider.resetStream = _resetStreamController.stream as Stream<bool>;
    super.initState();
  }

  @override
  void dispose() {
    _resetStreamController.close();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Container(
        padding: EdgeInsets.only(left: 16, right: 16, bottom: 8),
        child: Column(
          children: [
            BottomSheetHead(hit: provider.title),
            Expanded(child: _buildContent()),
            BaseBottomSheetButtonBar(cancelText: '重置', completeText: '确定', onComplete: _onComplete, onCancel: _onReSet),
          ],
        ),
      ),
    );
  }

  /// 构建内容
  Widget _buildContent() {
    return Provider(
      create: (_) => provider,
      child: Consumer<BaseAdvancedFilterProvider>(
        builder: (BuildContext context, BaseAdvancedFilterProvider model, Widget child) {
          return ListView.separated(
            itemCount: provider.models.length,
            separatorBuilder: (BuildContext context, int index) => SizedBox(height: 16, child: Divider(height: 1)),
            itemBuilder: (context, index) {
              return catalog(model.models[index]);
            },
          );
        },
      ),
    );
  }

  /// 完成
  void _onComplete() {
    Map<String, dynamic> map = BaseFilterDataProvider.dataEmptyFilter(provider.getAllDataKeyMap());
    if (widget.onComplete != null) widget.onComplete(map);
    Navigator.of(context).pop();
  }

  /// 重置
  void _onReSet() {
    this._resetStreamController.sink.add(true);
    provider.dataSource.clear();
    // Navigator.of(context).pop();
  }

  /// 筛选目录
  Widget catalog(BaseAdvancedFilterModel model) {
    Map<BaseAdvancedFilterType, Widget> catalog = {
      BaseAdvancedFilterType.SingleChoice: AdvancedFilterSingleChoice(model: model),
      BaseAdvancedFilterType.MultipleChoice: AdvancedFilterMultiChoice(model: model),
      BaseAdvancedFilterType.InputDateRange: AdvancedFilterDateRange(model: model),
      BaseAdvancedFilterType.InputTextRange: AdvancedFilterInputTextRange(model: model),
      BaseAdvancedFilterType.InputNumRange: AdvancedFilterInputTextRange(model: model, keyboardType: TextInputType.numberWithOptions(decimal: true)),
      BaseAdvancedFilterType.InputText: AdvancedFilterInputText(model: model),
      BaseAdvancedFilterType.InputSearchSingleChoice: AdvancedFilterInputSearchSingleChoice(model: model),
      BaseAdvancedFilterType.InputSearchMultipleChoice: AdvancedFilterInputSearchSingleChoice(model: model),
    };
    return catalog[model.inputType];
  }
}
